package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnum;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.service.TbItemCatService;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Optional;

@RequestMapping("/itemCatController")
@Controller
public class ItemCatController {

    private Logger logger = LoggerFactory.getLogger(ItemCatController.class);

    @DubboReference
    private TbItemCatService tbItemCatService;

    @RequestMapping("/toShow")
    public String toShow(){
        return "/admin/item_cat";
    }
    @RequestMapping("/listItemCat")
    @ResponseBody
    public Result listItemCat(@RequestParam(defaultValue = "0") Long parentId, Model model){
        try {
            List<TbItemCat> list = tbItemCatService.listItemCat(parentId);
            if (list.size() == 0){
                return Result.response(ResultEnum.NOT_FOUND_lIST);
            }
            return Result.OK().setData(list);
//            return Optional.ofNullable(list).map(u -> Result.OK().setData(u)).orElse(Result.response(ResultEnum.NOT_FOUND_lIST));
        }catch (Exception e){
            logger.error("分类列表查询错误 {}",e);
            return Result.ER();
        }
    }
    @RequestMapping("/saveItemCat")
    @ResponseBody
    public Result saveItemCat(@RequestParam(defaultValue = "0") Long parentId,String name,Long typeId){
        try {
            tbItemCatService.saveItemCat(parentId,name,typeId);
            return Result.OK();
        }catch (Exception e){
            logger.error("分类列表添加错误 {}",e);
            return Result.ER();
        }
    }
    @RequestMapping("/selectItemById")
    @ResponseBody
    public Result selectItemById(Long id){
        try {
            TbItemCat tbItemCat = tbItemCatService.selectItemById(id);
            return Result.OK().setData(tbItemCat);
        }catch (Exception e){
            logger.error("分类列表查询错误 {}",e);
            return Result.ER();
        }
    }
    @RequestMapping("/updateItemCat")
    @ResponseBody
    public Result updateItemCat(Long id,String name,Long typeId){
        try {
            tbItemCatService.updateItemCat(id,name,typeId);
            return Result.OK();
        }catch (Exception e){
            logger.error("分类列表修改错误 {}",e);
            return Result.ER();
        }
    }
    @RequestMapping("/delItems")
    @ResponseBody
    public Result delItems(@RequestParam(value = "ids")List<Long> ids){
        try {
            tbItemCatService.delItems(ids);
            return Result.OK();
        }catch (Exception e){
            logger.error("分类列表修改错误 {}",e);
            return Result.ER();
        }
    }
}
